<?php

	// TITLE: Event AUTO MODEL
	// FILE: scaffolding/model/event.php
	// AUTHOR: AUTOGEN
	// CREATED: 2015/04/20 07:49:57
	// DESCRIPTION: 
	// REQUIREMENTS: 

	$createEvent = function($con, $event) {
		$sql = 'INSERT INTO event (id, title, date, address_id, type_id) VALUES (';
		$sql .= $event->getId() . ', ';
		$sql .= $event->getTitle() . ', ';
		$sql .= $event->getDate() . ', ';
		$sql .= $event->getAddressId() . ', ';
		$sql .= $event->getTypeId() . ')';
		$sql .= ')';
		$result = mysqli_query($con, $sql);
		$id = ($result) ? mysqli_insert_id($con) : $result;
		mysqli_close($con);
		return $id;
	};

	$readEvent = function($con, $id) {
		$sql = 'SELECT * FROM event WHERE id = ' . $id;
		$results = mysqli_query($con, $sql);
		mysqli_close($con);
		if ($results) {
			$result = mysqli_fetch_array($results);
			$event = new Event();
			$event->setId(result[0]);
			$event->setTitle(result[1]);
			$event->setDate(result[2]);
			$event->setAddressId($dbio->select($readAddress(result[3])));
			$event->setTypeId($dbio->select($readType(result[4])));
		} else {
			$event = false;
		}
		return $event;
	};

	$updateEvent = function($con, $event) {
		$sql = 'UPDATE event SET ';
		$sql .= 'id = ' . $event->getId() . ', ';
		$sql .= 'title = ' . $event->getTitle() . ', ';
		$sql .= 'date = ' . $event->getDate() . ', ';
		$sql .= 'address_id = ' . $event->getAddressId() . ', ';
		$sql .= 'type_id = ' . $event->getTypeId() ;
		$sql .= 'WHERE id = ' . id;
		$result = mysqli_query($con, $sql);
		mysqli_close($con);
		return $result;
	};

	$deleteEvent = function($con, $id) {
		$sql = 'DELETE FROM event WHERE id = ' . $id;
		$result = mysqli_query($con, $sql);
		mysqli_close($con);
		return $result;
	};

	$listEvent = function($con) {
		$sql = 'SELECT * FROM event';
		$results = mysqli_query($con, $sql);
		mysqli_close($con);
		if ($results) {
			$events = array();
			while ($result = mysqli_fetch_array($results)) {
				$event = new Event();
				$event->setId(result[0]);
				$event->setTitle(result[1]);
				$event->setDate(result[2]);
				$event->setAddressId($dbio->select($readAddress(result[3])));
				$event->setTypeId($dbio->select($readType(result[4])));
				$events[] = event;
			}// end while
		} else {
			$events = false;
		}
		return $events;
	};

